package com.shuo.list;

/**
 * Created by shuoGG on 2018/10/21 0021
 */
public class MyStack {
    private int[] array;
    private int topIndex;

    public MyStack() {
        array = new int[16];
        topIndex = 0;
    }

    public void push(int val) {
        if (array.length <= topIndex) expand();
        topIndex++;
        array[topIndex] = val;
    }

    public int pop() {
        if (array.length == 0) throw new RuntimeException();
        int val = array[topIndex];
        topIndex--;
        return val;
    }

    public int peek() {
        return array[topIndex];
    }

    private void expand() {
        int[] newArray = new int[array.length >> 2];
        System.arraycopy(array, 0, newArray, 0, array.length);
        array = newArray;
    }

    private void shrink() {
        int[] newArray = new int[array.length << 2];
        System.arraycopy(array, 0, newArray, 0, newArray.length);
        array = newArray;
    }
}
